from openai import OpenAI


def get_ai_btn_mate(user_massage):

    return get_ai(user_massage)

def get_ai(user_massage):

    API_SECRET_KEY = "ZQbZgLCSS92qz"
    # API_SECRET_KEY = "sk-2xyIpkITIFhDOABIA8266f64Fd08414eAfE13209E4Ab004f"

    BASE_URL = "https://ai.liaobots.work/v1/"
    # BASE_URL = "https://api.gpts.vin/v1/"

    messages = []

    prompt_words = construct_prompt_words(user_massage)

    # 添加消息
    messages.append({"role": 'user', "content": prompt_words})


    # app.logger.info(messages)

    try:
        client = OpenAI(api_key=API_SECRET_KEY, base_url=BASE_URL)
        resp = client.chat.completions.create(
            # model="claude-3-5-sonnet-20240620",
            # model="gpt-4o-mini",
            # model="gpt-4o",
            # model="grok-2-mini",
            # model="grok-2",
            # model="gpt-3.5-turbo",
            # model="gpt-4-0613",
            model="gemini-1.5-pro-latest",
            messages=messages
        )

        # app.logger.info(resp)
        # app.logger.info(resp.choices[0].message.content)
        # app.logger.info("请求已成功")

        return resp.choices[0].message.content.replace("\n", "").replace(" ", "")

    except Exception as e:
        # app.logger.info("请求失败:", e)

        print("请求失败:", e)

        return "请求失败"



def construct_prompt_words(user_massage):
    # 打开文件
    with open('ren_btn.txt', 'r', encoding='utf-8') as f:
        # 读取所有内容
        ren_btn_text = f.read()

    prompt_words = \
    "漂流瓶内容：\n"\
    + ren_btn_text\
    +"\n" \
    + "以上是我飘出的瓶子内容\n" \
    + f"下面我收到一条来自男生的回复：{user_massage}\n"\
    +"请帮我找出他最有可能是捡到了我的哪条内容，只需要输出内容，不准有多余的语言"

    return prompt_words


if __name__ == "__main__":

    # print(get_ai_btn_mate("sdfie"))

    # construct_prompt_words("77")

    print(get_ai_btn_mate("可以曹你的sao币吗"))

